﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>脚本</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    @Html.Style("~/scripts/layuiadmin/layui/css/layui.css")
    @Html.Style("~/scripts/layuiadmin/style/admin.css")
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="query-filter">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline" style="width:170px;">
                            <input type="text" class="layui-input" name="name" placeholder="请输入关键词" />
                        </div>
                    </div>
                    <div class="layui-inline" style="width:100px;">
                        <select name="scriptType">
                            <option value="0">类型</option>
                            <option value="1">IEC</option>
                            <option value="2">ZIP</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn" id="btnSearch">搜索</button>
                        <button class="layui-btn" id="btnAdd">添加</button>
                    </div>
                </div>
            </div>
            <div class="layui-card-body">
                <table id="LAY-app-script-list" lay-filter="LAY-app-script-list"></table>
            </div>
        </div>
    </div>
    <script type="text/html" id="script-edit">
        <div class="layui-form" lay-filter="script-edit" style="margin:10px;">
            <div class="layui-form-item">
                <label class="layui-form-label">脚本类型</label>
                <div class="layui-input-block">
                    <select name="scriptType">
                        <option value="2">ZIP</option>
                        <option value="1">IEC</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">可用用户</label>
                <div class="layui-input-block">
                    <select name="userName">
                        <option value="">所有用户</option>
                        @foreach (var item in ViewBag.Users)
                        {
                            <option value="@item.UserName">@item.UserName</option>
                        }
                        <option value="均不可用">均不可用</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="hidden" name="action" value="add">
                    <input type="hidden" name="id" value="0">
                    <input type="text" name="name" id="name" class="layui-input" lay-verify="required" placeholder="请输入名称" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">版本名称</label>
                <div class="layui-input-block">
                    <input type="text" name="versionName" id="versionName" class="layui-input" lay-verify="required" placeholder="请输入版本名称" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">版本编码</label>
                <div class="layui-input-block">
                    <input type="text" name="versionCode" id="versionCode" class="layui-input" lay-verify="required" placeholder="请输入版本编码" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">下载地址</label>
                <div class="layui-inline" style="width:370px;">
                    <input type="text" name="downloadUrl" id="downloadUrl" class="layui-input" lay-verify="required" placeholder="请输入下载地址（要确定http访问可直接下载）" />
                </div>
                <div class="layui-inline" style="width:50px;">
                    <button type="button" class="layui-btn" id="btnUpload">上传</button>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">文件MD5</label>
                <div class="layui-input-block">
                    <input type="text" name="fileMd5" id="fileMd5" class="layui-input" lay-verify="required" placeholder="请输入脚本校验MD5" />
                </div>
            </div>
            @*<div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="scriptStatus" value="1" title="启用" lay-filter="scriptStatus">
                <input type="radio" name="scriptStatus" value="2" title="禁用" lay-filter="scriptStatus" checked>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">自动更新</label>
            <div class="layui-input-block">
                <input type="radio" name="autoUpgrade" value="1" title="启用" disabled>
                <input type="radio" name="autoUpgrade" value="2" title="禁用" disabled checked>
            </div>
        </div>*@
        </div>
    </script>

    @Html.Script("~/scripts/layuiadmin/layui/layui.js")
    @Html.Script("~/scripts/qiniu.min.js")
    @Html.Script("~/scripts/clipboard.min.js")
<script>
        layui.config({
            base: '../../scripts/layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'upload'], function () {
            var table = layui.table,
                upload = layui.upload,
                form = layui.form,
                $ = layui.$;

            initData();
            $('#btnAdd').click(function () { showEditDialog(undefined) });
            $('#btnSearch').click(initData);

            form.on('radio(scriptStatus)', function (data) {
                if (data.value == 1) {
                    $('input[name="autoUpgrade"]').removeAttr('disabled');
                } else {
                    $('input[name="autoUpgrade"]:last').prop('checked', true);
                    $('input[name="autoUpgrade"]').attr('disabled', 'disabled');
                }

                form.render();
            });

            table.on('tool(LAY-app-script-list)', function (obj) {
                var layEvent = obj.event,
                    data = obj.data;

                if (layEvent === 'delete') {
                    layer.confirm('确定要删除？', {
                        title: '提示',
                        btn: ['确定', '取消']
                    }, function (index) {
                        $.post('/admin/script/delete', { id: data.id }, function (result) {
                            if (result.code == 1) {
                                layer.close(index);
                                table.reload('scriptListTable');
                            } else {
                                layer.msg(result.msg);
                            }
                        });
                    });
                } else if (layEvent === 'update') {
                    data.action = "edit";
                    showEditDialog(data);
                }
            });

            function initData() {
                table.render({
                    elem: '#LAY-app-script-list',
                    url: '/admin/script/list-page',
                    cellMinWidth: 95,
                    method: 'post',
                    page: true,
                    limit: 20,
                    limits: [20, 50, 100],
                    id: "scriptListTable",
                    where: form.val("query-filter"),
                    response: {
                        statusName: 'code'
                        , statusCode: 1
                        , msgName: 'msg'
                        , countName: 'count'
                        , dataName: 'data'
                    },
                    cols: [[
                        { field: 'id', title: 'id', width: 60 },
                        { field: 'name', title: '名称', width: 160 },
                        { field: 'versionName', title: '版本号', width: 100 },
                        { field: 'versionCode', title: '版本代码', width: 100 },
                        {
                            field: 'downloadUrl', title: '下载地址', templet: function (d) {
                                return '<div title="点击复制" data-clipboard-text="' + d.downloadUrl + '" class="key-clipboard" style="cursor:pointer;">' + d.downloadUrl + '<i class="layui-icon layui-icon-file-b"></i></div>'
                            }
                        },
                        {
                            field: 'fileMd5', title: '文件MD5', width: 300, templet: function (d) {
                                return '<div title="点击复制" data-clipboard-text="' + d.fileMd5 + '" class="key-clipboard" style="cursor:pointer;">' + d.fileMd5 + '<i class="layui-icon layui-icon-file-b"></i></div>'
                            }
                        },
                        {
                            field: 'scriptType', title: '类型', width: 90, templet: function (d) {
                                if (d.scriptType == 1) {
                                    return 'IEC'
                                } else {
                                    return 'ZIP'
                                }
                            }
                        },
                        { field: 'addTime', title: '添加时间', width: 170 },
                        {
                            title: '操作', width: 120, align: "center", templet: function (d) {
                                return '<div><button class="layui-btn layui-btn-xs" lay-event="update">编辑</button><button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</button><div>';
                            }
                        }
                    ]],
                    done: function () {
                        var btns = document.querySelectorAll('.key-clipboard');
                        var clipboard = new ClipboardJS(btns);

                        clipboard.on('success', function (e) {
                            layer.msg('已复制');
                        });
                    }
                });
            }

            function showEditDialog(script) {
                var url = script ? "/admin/script/edit" : "/admin/script/add";
                layer.open({
                    type: 1
                    , title: script ? '编辑' : '添加'
                    , content: $('#script-edit').html()
                    , area: ['600px','500px']
                    , btn: ['确定', '版本+1', '取消']
                    , success: function () {
                        if (script) {
                            form.val("script-edit", script);

                            if (script.scriptStatus == 1) {
                                $('input[name="autoUpgrade"]').removeAttr('disabled');
                            }
                        }

                        form.render();

                        upload.render({
                            elem: '#btnUpload'
                            , accept: 'file'
                            , url: '123'
                            , choose: function (obj) {
                                obj.preview(function (index, file, result) {
                                    var ext = file.name ? file.name.substr(file.name.lastIndexOf('.')) : '';
                                    $.post('/admin/qiniu-token', { ext: ext}, function (res) {
                                        var that = this;

                                        if (res.code == 1) {
                                            var putExtra = {};
                                            var observable = qiniu.upload(file, res.msg, res.data, putExtra, {
                                                region: qiniu.region.z0
                                            });
                                            observable.subscribe(function (ress) {
                                                console.log(ress);
                                            }, function (error) {
                                                layer.msg('上传失败，请重试');
                                            }, function (ress) {
                                                $('#downloadUrl').val('@ViewBag.QiniuOriginDomain' + ress.key);
                                            });
                                            return;
                                        } else {
                                            layer.msg(res.msg);
                                        }

                                        return;
                                    });
                                });
                            }
                        });
                    }
                    , yes: function (index, layero) {
                        var formData = form.val("script-edit");

                        $.post(url, formData, function (data) {
                            if (data.code == 1) {
                                layer.close(index);
                                table.reload('scriptListTable');
                            } else {
                                layer.msg(data.msg);
                            }
                        });
                    }, btn2: function (index, layero) {
                        var $versionName = $('#versionName');
                        var $versionCode = $('#versionCode');

                        $versionName.val(getNew($versionName.val()));
                        $versionCode.val(getNew($versionCode.val()));
                        return false;
                    }, btn3: function (index, layero) {
                        layer.close(index);
                    }
                });
            }
        });

        function getNew(txt) {
            var reg = /\d+/g;
            var res = txt.match(reg);

            if (res.length > 0) {
                var last = res[res.length - 1];
                txt = txt.replace(last, padLeft((parseInt(last) + 1) + '', 2, '0'));
            }

            return txt;
        }

        function padLeft(txt, length, char) {
            if (txt.length < length) {
                txt = (Array(length).join(char) + txt).slice(-length);
            }

            return txt;
        }
</script>
</body>
</html>